﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0076)http://www.cnblogs.com/greenerycn/archive/2009/11/18/windows_auto_logon.html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><HTML 
lang="zh-cn" xmlns="http://www.w3.org/1999/xhtml"><HEAD><META 
content="IE=9.0000" http-equiv="X-UA-Compatible">

<META content="text/html; charset=utf-8" http-equiv="Content-Type">
<TITLE>编程实现Windows系统自动登录 - 小杰 - 博客园</TITLE><LINK rel="stylesheet" type="text/css" 
href="编程实现Windows系统自动登录%20-%20小杰%20-%20博客园_files/blog-common.css"><LINK id="MainCss" 
rel="stylesheet" type="text/css" href="编程实现Windows系统自动登录%20-%20小杰%20-%20博客园_files/bundle-SimpleBlue.css">
<LINK rel="stylesheet" type="text/css" href="编程实现Windows系统自动登录%20-%20小杰%20-%20博客园_files/32863.css">
<LINK title="RSS" rel="alternate" type="application/rss+xml" href="http://www.cnblogs.com/greenerycn/rss">
<LINK title="RSD" rel="EditURI" type="application/rsd+xml" href="http://www.cnblogs.com/greenerycn/rsd.xml">
<LINK rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://www.cnblogs.com/greenerycn/wlwmanifest.xml">
<SCRIPT type="text/javascript" src="编程实现Windows系统自动登录%20-%20小杰%20-%20博客园_files/jquery.js"></SCRIPT>

<SCRIPT type="text/javascript">var currentBlogApp = 'greenerycn', cb_enable_mathjax=false;</SCRIPT>

<SCRIPT type="text/javascript" src="编程实现Windows系统自动登录%20-%20小杰%20-%20博客园_files/blog-common.js"></SCRIPT>

<META name="GENERATOR" content="MSHTML 9.00.8112.16636"></HEAD>
<BODY><A name="top"></A>
<DIV id="home">
<DIV id="header">
<DIV id="blogTitle"><!--done-->
<DIV class="title"><A id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/greenerycn/">LinJie's 
Blog</A></DIV>
<DIV class="subtitle">在Coding路上前进的人</DIV></DIV><!--end: blogTitle 博客的标题和副标题 -->
<DIV id="navigator">
<UL id="navList">
  <LI id="nav_sitehome"><A id="MyLinks1_HomeLink" class="menu" href="http://www.cnblogs.com/">博客园</A></LI>
  <LI id="nav_myhome"><A id="MyLinks1_MyHomeLink" class="menu" href="http://www.cnblogs.com/greenerycn/">首页</A></LI>
  <LI id="nav_q"><A class="menu" href="http://q.cnblogs.com/">博问</A></LI>
  <LI id="nav_ing"><A class="menu" 
  href="http://home.cnblogs.com/ing/">闪存</A></LI>
  <LI id="nav_newpost"><A id="MyLinks1_NewPostLink" class="menu" href="http://i.cnblogs.com/EditPosts.aspx?opt=1" 
  rel="nofollow">新随笔</A></LI>
  <LI id="nav_contact"><A id="MyLinks1_ContactLink" class="menu" href="http://space.cnblogs.com/msg/send/%e5%b0%8f%e6%9d%b0" 
  rel="nofollow">联系</A></LI>
  <LI id="nav_rss"><A id="MyLinks1_Syndication" class="menu" href="http://www.cnblogs.com/greenerycn/rss">订阅</A><!--<a id="MyLinks1_XMLLink" class="aHeaderXML" href="http://www.cnblogs.com/greenerycn/rss"><img src="http://www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--></LI>
  <LI id="nav_admin"><A id="MyLinks1_Admin" class="menu" href="http://i.cnblogs.com/" 
  rel="nofollow">管理</A></LI></UL>
<DIV class="blogStats"><!--done-->随笔-98&nbsp;文章-4&nbsp;评论-303&nbsp;					</DIV><!--end: blogStats --></DIV><!--end: navigator 博客导航栏 -->
</DIV><!--end: header 头部 -->
<DIV id="main">
<DIV id="mainContent">
<DIV class="forFlow"><!--done-->
<DIV id="topics">
<DIV class="post">
<H1 class="postTitle"><A id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/greenerycn/archive/2009/11/18/windows_auto_logon.html">编程实现Windows系统自动登录</A></H1>
<DIV class="clear"></DIV>
<DIV class="postBody">
<DIV id="cnblogs_post_body">
<CENTER>
<H3>编程实现Windows系统自动登录</H3></CENTER>
<P><B>原理</B>：
<P>
<P>     
通过注册表修改实现。Windows内置了自动登录的机制，在登录系统时，winlogon会检查注册表下有没有设置自动登录，如果设置了就上就会读取用户名和密码，然后进行验证，通过后就登录进系统了。
</P>
<P><B>方法</B>：</P>
<P>    注册表项：HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows 
NT\CurrentVersion\Winlogon</P>
<P>键值：
<P>
<TABLE border="1">
  <TBODY>
  <TR>
    <TD>键值</TD>
    <TD>设置值</TD>
    <TD>作用</TD></TR>
  <TR>
    <TD>AutoAdminLogon</TD>
    <TD>1;</TD>
    <TD>1表示自动登录，0表示禁止自动登录</TD></TR>
  <TR>
    <TD>DefaultDomainName   </TD>
    <TD>设置值</TD>
    <TD>域名，如果没有就留空</TD></TR>
  <TR>
    <TD>DefaultUserName       </TD>
    <TD>abc</TD>
    <TD>登录系统用的用户名</TD></TR>
  <TR>
    <TD>DefaultPassword        </TD>
    <TD>123</TD>
    <TD>登录系统用的用户名使用的密码,如果想加密保存就删除此键值</TD></TR>
  <TR>
    <TD>DontDisplayLastUserName </TD>
    <TD>1</TD>
    <TD>&nbsp;</TD></TR></TBODY></TABLE>
<P>注意，下面两个键值必须删除掉，否则会显示一个通知对话框。<BR>
<UL>
  <LI>LegalNoticeCaption</LI>
  <LI>LegalNoticeText</LI></UL>
<P></P><BR>
<P>另外还有2个键值，作用分别如下：<BR>
<UL>
  <LI> IgnoreShiftOverride    
  忽略shift键，默认在开机时按shift机会显示登录对话框（即使设置成自动登录），设置成1后，按shift键也没用了。</LI>
  <LI> AutoLogonCount   设置自动登录次数</LI>
  <LI>ForceAutoLogon  该值要删除掉</LI></UL>
<P></P>
<P><B>特别注意</B>：</P>
<P>   
由于密码是明文保存的，很不安全，所以windows中提供了一个秘密的地方来保存加密后的密码，就是在lsa中。sysinternals中autologon工具就是把密码保存进去的。 
在Winlogon中，它会先到lsa中用找“DefaultPassword”的信息，找到后就用这个密码，如果找不到就到注册表下去找“DefaultPassword”值。
</P>
<P>读写lsa的代码，这个代码是pinvoke.net 
中找到的。原文：<BR>http://www.pinvoke.net/default.aspx/advapi32/DefaultPassword.html
</P>
<P>其中C++代码由微软提供：http://msdn.microsoft.com/en-us/library/aa378826(VS.85).aspx<BR>
C#代码为：http://www.pinvoke.net/default.aspx/advapi32/LsaRetrievePrivateData.html
<BR>获取LUAUtil类后，如下调用即可，<PRE>LSAutil lsa = new LSAutil("DefaultPassword");
lsa.SetSecret(“abc”);
Console.Write(lsa.GetSecret());
</PRE>
<P></P></DIV>
<DIV id="MySignature"></DIV>
<DIV class="clear"></DIV>
<DIV id="blog_post_info_block">
<DIV id="BlogPostCategory"></DIV>
<DIV id="EntryTag"></DIV>
<DIV id="blog_post_info"></DIV>
<DIV class="clear"></DIV>
<DIV id="post_next_prev"></DIV></DIV></DIV>
<DIV class="postDesc">posted @ <SPAN id="post-date">2009-11-18 22:38</SPAN> <A 
href="http://www.cnblogs.com/greenerycn/">小杰</A> 阅读(<SPAN 
id="post_view_count">...</SPAN>) 评论(<SPAN id="post_comment_count">...</SPAN>)  
<A href="http://i.cnblogs.com/EditPosts.aspx?postid=1605692" 
rel="nofollow">编辑</A> <A onclick="AddToWz(1605692);return false;" href="http://www.cnblogs.com/greenerycn/archive/2009/11/18/windows_auto_logon.html#">收藏</A></DIV></DIV>
<SCRIPT type="text/javascript">var allowComments=true,isLogined=false,cb_blogId=32863,cb_entryId=1605692,cb_blogApp=currentBlogApp,cb_blogUserGuid='568c3d0b-63cf-dd11-9e4d-001cf0cd104b',cb_entryCreatedDate='2009/11/18 22:38:00';loadViewCount(cb_entryId);</SCRIPT>
</DIV><!--end: topics 文章、评论容器--><A name="!comments"></A>
<DIV id="blog-comments-placeholder"></DIV>
<SCRIPT type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</SCRIPT>

<DIV id="comment_form" class="commentform"><A name="commentform"></A>
<DIV id="divCommentShow"></DIV>
<DIV id="comment_nav"><SPAN id="span_refresh_tips"></SPAN><A id="lnk_RefreshComments" 
onclick="return RefreshCommentList();" href="javascript:void(0);">刷新评论</A><A 
onclick="return RefreshPage();" href="http://www.cnblogs.com/greenerycn/archive/2009/11/18/windows_auto_logon.html#">刷新页面</A><A 
href="http://www.cnblogs.com/greenerycn/archive/2009/11/18/windows_auto_logon.html#top">返回顶部</A></DIV>
<DIV id="comment_form_container"></DIV>
<DIV id="ad_text_under_commentbox" class="ad_text_commentbox"></DIV>
<DIV id="site_nav_under"><A title="开发者的网上家园" href="http://www.cnblogs.com/" 
target="_blank">博客园首页</A><A title="程序员问答社区" href="http://q.cnblogs.com/" target="_blank">博问</A><A 
title="IT新闻" href="http://news.cnblogs.com/" target="_blank">新闻</A><A href="http://home.cnblogs.com/ing/" 
target="_blank">闪存</A><A href="http://job.cnblogs.com/" 
target="_blank">程序员招聘</A><A href="http://kb.cnblogs.com/" 
target="_blank">知识库</A></DIV>
<DIV id="opt_under_post"></DIV>
<SCRIPT type="text/javascript">var enableGoogleAd = canShowAdsense(); var googletag = googletag || {}; googletag.cmd = googletag.cmd || [];
    fixPostBodyFormat();
</SCRIPT>

<DIV id="ad_under_post_holder">
<SCRIPT type="text/javascript">
    var googletag = googletag || {};
    googletag.cmd = googletag.cmd || [];
    (function () {
        if (enableGoogleAd) {
            var gads = document.createElement('script');
            gads.async = true;
            gads.type = 'text/javascript';
            var useSSL = 'https:' == document.location.protocol;
            gads.src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
            var node = document.getElementsByTagName('script')[0];
            node.parentNode.insertBefore(gads, node);
        }
    })();
</SCRIPT>

<SCRIPT type="text/javascript">
    try {
        if (enableGoogleAd) {
            googletag.cmd.push(function () {
                googletag.defineSlot('/1090369/cnblogs_blogpost_C1_sitehome', [300, 250], 'div-gpt-ad-1346480159711-0').addService(googletag.pubads());
                googletag.defineSlot('/1090369/cnblogs_blogpost_C2', [468, 60], 'div-gpt-ad-1410860226396-0').addService(googletag.pubads());
                googletag.pubads().enableSingleRequest();
                googletag.enableServices();
            });
        };
    } catch (e) { }
</SCRIPT>

<DIV id="google_ad_c1" class="c_ad_block">
<DIV style="width: 300px; height: 250px;" id="div-gpt-ad-1346480159711-0">
<SCRIPT type="text/javascript">
        try {
            if (enableGoogleAd) {
                googletag.cmd.push(function () { googletag.display('div-gpt-ad-1346480159711-0'); });            
            } else {
                $('#div-gpt-ad-1346480159711-0').hide();
            }
    } catch (e) { }
    </SCRIPT>
</DIV></DIV></DIV>
<DIV id="under_post_news"></DIV>
<DIV id="google_ad_c2" class="c_ad_block">
<DIV style="width: 468px; height: 60px;" id="div-gpt-ad-1410860226396-0">
<SCRIPT type="text/javascript">
try {
    if (enableGoogleAd) {
        googletag.cmd.push(function () { googletag.display('div-gpt-ad-1410860226396-0'); });
    } else {
        $('#div-gpt-ad-1346480159711-0').hide();
    }
} catch (e) { }
</SCRIPT>
</DIV></DIV>
<DIV id="under_post_kb"></DIV>
<DIV id="HistoryToday" class="c_ad_block"></DIV>
<SCRIPT type="text/javascript">
$(function () {
    loadNewsAndKb();
    loadBlogSignature();
    LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
    GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate);
    loadOptUnderPost();
    GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
    setTimeout(function () { incrementViewCount(cb_entryId); }, 200);
});
</SCRIPT>
</DIV></DIV><!--end: forFlow --></DIV><!--end: mainContent 主体内容容器-->
<DIV id="sideBar">
<DIV id="sideBarMain"><!--done-->
<DIV class="newsItem">
<H3 class="catListTitle">公告</H3>
<DIV id="blog-news"></DIV>
<SCRIPT type="text/javascript">loadBlogNews();</SCRIPT>
</DIV>
<DIV id="calendar">
<DIV style="display: none;" id="blog-calendar"></DIV>
<SCRIPT type="text/javascript">loadBlogDefaultCalendar();</SCRIPT>
</DIV>
<DIV id="leftcontentcontainer">
<DIV id="blog-sidecolumn"></DIV>
<SCRIPT type="text/javascript">loadBlogSideColumn();</SCRIPT>
</DIV></DIV><!--end: sideBarMain --></DIV><!--end: sideBar 侧边栏容器 -->
<DIV class="clear"></DIV></DIV><!--end: main -->
<DIV class="clear"></DIV>
<DIV id="footer"><!--done-->Copyright ©2015 小杰	</DIV><!--end: footer --></DIV><!--end: home 自定义的最大容器 --><!--PageEndHtml Block Begin-->
<SCRIPT type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</SCRIPT>

<SCRIPT type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-1084938-5");
pageTracker._trackPageview();
} catch(err) {}</SCRIPT>
<!-- Baidu -->
<SCRIPT type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Fc1f53b67298e0764d5ff36e70b2b4c63' type='text/javascript'%3E%3C/script%3E"));
</SCRIPT>
<!--PageEndHtml Block End--></BODY></HTML>
